<?php
namespace app\api\controller;
use think\Db;
use app\common\model\OnetooneBaseinfo as OnetooneBaseinfoModel;


//一对一
class Onetoone extends App
{
    
	
    //基础信息保存
    public function save_base_info()
    {
        $user_id = $this->user_id;
        if($this->request->isPost()){
			$data = $this->request->post();
			if( empty($data['order_id'])){
				return $this->show(2001,"缺少order_id", '');
			}
			$course_order = Db::name("course_order")->where('id',$data['order_id'])->where('user_id',$user_id)->where('type',2)->where('status',2)->where('course_id',2)->field('id,zhuanjia_id')->find();
			if( empty($course_order)){
				return $this->show(2001,"您还没购买一对一服务", '');
			}
			
			if(empty($data['realname']) || empty($data['sex']) || empty($data['nation']) || empty($data['year']) || empty($data['province_id'])){
				return $this->show(2001,"请填写完整", '');
			}
			
			$flag = Db::name("onetoone_baseinfo")->where('order_id',$data['order_id'])->where('user_id',$user_id)->value('id');
			if(empty($flag)){
				$obj = new OnetooneBaseinfoModel;
				$data['user_id'] = $user_id;
				$data['zhuanjia_id'] = $course_order['zhuanjia_id'];
                $res = $obj->allowField(true)->save($data);
				$base_id = $obj->id;
			}else{
				$obj = new OnetooneBaseinfoModel;
				$data['zhuanjia_id'] = $course_order['zhuanjia_id'];
				$res = $obj->allowField(true)->save($data, ['id' => $flag]);
				$base_id = $flag;
			}
			Db::name("course_order")->where('id',$data['order_id'])->update(['base_id'=>$base_id,'is_base'=>1,'base_time'=>time()]);
			
			if($res !== false){
				$data_return = ['base_id'=>$base_id];
				return $this->show(1,"success", $data_return);
			}else{
				return $this->show(2002, "fail", "");
			}
		}else{
            return $this->show(2001,"非法请求", '');
        }	
    }
	
	
    public function get_base_info()
    {
        
        $user_id = $this->user_id;
        $order_id = $this->request->get("order_id",0,'intval'); 
        if(empty($order_id)){
            return $this->show(2001,"缺少order_id!", '');
        }
        $info = Db::name("onetoone_baseinfo")->where('user_id',$user_id)->where('order_id',$order_id)->find();
		 if(empty($info)){
            return $this->show(2001,"数据不存在或已下架!", '');
        }
		$info['intention_major'] = Db::name("major")->where('id','in',$info['intention_major_ids'])->field('id,cate_title')->select(); 
		$info['exclude_major'] = Db::name("major")->where('id','in',$info['exclude_major_ids'])->field('id,cate_title')->select(); 
		
		unset($info['status']);	
		unset($info['create_time']);	
		unset($info['update_time']);	

			
		return $this->show(1,"成功", $info);
		
    }
	
    public function get_order_status()
    {
        
        $user_id = $this->user_id;
        $order_id = $this->request->get("order_id",0,'intval'); 
        if(empty($order_id)){
            return $this->show(2001,"缺少order_id!", '');
        }
        $course_order = Db::name("course_order")->where('id',$order_id)->where('user_id',$user_id)->where('type',2)->where('status',2)->where('course_id',2)->field('id,is_base,is_zhiyuan,is_finish')->find();
		if( empty($course_order)){
			return $this->show(2001,"您还没购买一对一服务", '');
		}
			
		return $this->show(1,"成功", $course_order);
		
    }
	
	//一对一服务-获取专家制定的志愿方案
    public function get_fangan()
    {
        
        $user_id = $this->user_id;
        $order_id = $this->request->get("order_id",0,'intval'); 
        if(empty($order_id)){
            return $this->show(2001,"缺少order_id!", '');
        }
        $info = Db::name("course_order")->where('user_id',$user_id)->where('id',$order_id)->find();
		if(empty($info)){
            return $this->show(2001,"数据不存在或已下架!", '');
        }	

		$zhiyuan = json_decode($info['yuanxi'],true);//dump($zhiyuan);
		foreach($zhiyuan as $k=>$v){
			$zhiyuan[$k]['majors'] = Db::name("major")->where('id','in',$v['major_ids'])->field('id,cate_title')->select();
			unset($zhiyuan[$k]['major_ids']);
			//foreach($v['major_ids'] as $k1=>$v1){	
			//}
		}//dump($zhiyuan);
		$yuanxi_time = !empty($info['yuanxi_time'])?date('Y-m-d H:i:s',$info['yuanxi_time']):'';
		$baogao_file = !empty($info['baogao_file'])?config('app_host').$info['baogao_file']:'';
			
		return $this->show(1,"成功", ['zhiyuan'=>$zhiyuan,'baogao_file'=>$baogao_file,'zhiyuan_time'=>$yuanxi_time]);
		
    }
	
	public function finish_fangan()
    {
        $user_id = $this->user_id;
        if($this->request->isPost()){
			$data = $this->request->post();
			if(empty($data['order_id'])){
				return $this->show(2001,"缺少order_id", '');
			}
			$course_order = Db::name("course_order")->where('id',$data['order_id'])->where('user_id',$user_id)->where('type',2)->where('course_id',2)->field('id,status')->find();
			//dump(Db::name("course_order")->getlastsql());die;
			if( empty($course_order) || $course_order['status']!=2){
				return $this->show(2001,"非法操作", '');
			}
	
			$res = Db::name("course_order")->where('id',$data['order_id'])->update(['is_finish'=>1,'finish_time'=>time()]);
			
			if($res !== false){
				return $this->show(1,"success");
			}else{
				return $this->show(2002, "fail", "");
			}
		}else{
            return $this->show(2001,"非法请求", '');
        }	
    }
}